package com.scsj.supermarket.adapter;


import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.MineCouponBean;
import com.scsj.supermarket.utils.SkipUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 优惠券
 */

public class CouponAdapter extends BaseQuickAdapter<MineCouponBean.DataBean.ListBean, BaseViewHolder> {

    private int type = 0;// 0 未使用  1 已使用  2 已过期
    private Context mContext;
    public CouponAdapter(Context mContext,List data,int type) {
        super(R.layout.item_coupon, data);
        this.mContext = mContext;
        this.type = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, final MineCouponBean.DataBean.ListBean item) {

        TextView coupon_button = helper.getView(R.id.coupou_use_now_tv);

        if(type == 0){
            helper.setText(R.id.coupou_discounts_price_tv,item.getMoney()+"");
            helper.setText(R.id.coupou_discounts_price_tv,"满"+item.getUseCondition()+"元使用");

            helper.getView(R.id.background_ll).setBackgroundResource(R.mipmap.unused);
            helper.setText(R.id.coupou_time_tv,item.getEndTime()+"到期");
            helper.setText(R.id.coupou_reduce_time_tv,"(仅剩"+getDay(item)+"天)");
            coupon_button.setVisibility(View.VISIBLE);
            coupon_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2018/7/6 跳转到对应的店铺中
                    SkipUtils.toMarket(mContext,item.getStoreId());
                }
            });
           helper.setText(R.id.store_name_tv,"仅限在"+item.getStoreName()+"使用");

        }else if(type == 1){
            helper.getView(R.id.background_ll).setBackgroundResource(R.mipmap.has_used);
            coupon_button.setVisibility(View.GONE);
            helper.getView(R.id.coupou_reduce_time_tv).setVisibility(View.GONE);
            helper.setText(R.id.coupou_discounts_price_tv,item.getMoney()+"");
            helper.setText(R.id.coupou_discounts_price_tv,"满"+item.getUseCondition()+"元使用");
            helper.setText(R.id.coupou_time_tv,item.getEndTime()+" 已使用");
            helper.setText(R.id.store_name_tv,"仅限在"+item.getStoreName()+"使用");

        }else if(type == 2){
            helper.getView(R.id.background_ll).setBackgroundResource(R.mipmap.has_used);
            coupon_button.setVisibility(View.GONE);
            helper.getView(R.id.coupou_reduce_time_tv).setVisibility(View.GONE);
            helper.setText(R.id.coupou_discounts_price_tv,item.getMoney()+"");
            helper.setText(R.id.coupou_discounts_price_tv,"满"+item.getUseCondition()+"元使用");
            helper.setText(R.id.coupou_time_tv,item.getEndTime()+" 已过期");
            helper.setText(R.id.store_name_tv,"仅限在"+item.getStoreName()+"使用");
        }
    }

    private BigDecimal getDay(MineCouponBean.DataBean.ListBean item) {
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss");
        Date startDate = null;
        Date endDate = null;
        try {
            startDate=inputFormat.parse(item.getBeginTime());
            endDate=inputFormat.parse(item.getEndTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //得到两个日期对象的总毫秒数
        long firstDateMilliSeconds = startDate.getTime();
        long secondDateMilliSeconds = endDate.getTime();

        //得到两者之差
        long firstMinusSecond = secondDateMilliSeconds - firstDateMilliSeconds;
        //得到秒*60*60*24
        long totalDays = (firstMinusSecond / 1000*60*60*24);
        BigDecimal bigDecimal = new BigDecimal(totalDays).setScale(1, BigDecimal.ROUND_HALF_UP);
        return bigDecimal;
    }
}
